# !/usr/bin/env python
# coding=utf-8
# @Time    : 2017/12/19 16:16
# @Author  : 6imensiva
# @Site    : https://gitee.com/dimensiva/wx_server_prototype.git
# @File    : base_callback.py
# @Software: PyCharm
# @Version: 3.5.4

from flask import request
from configs import current_config
from utils import XML2Dict
from . import api


@api.route(current_config.API_PREFIX + '/base_callback', methods=['GET'])
def handle_get_callback():
	"""
	微信验证服务器地址的有效性回调

	:return:
	"""
	signature = request.args.get('signature')
	timestamp = request.args.get('timestamp')
	nonce = request.args.get('nonce')
	echostr = request.args.get('echostr')
	print(signature, timestamp, nonce, echostr)
	return echostr


@api.route(current_config.API_PREFIX + '/base_callback', methods=['POST'])
def handle_post_callback():
	"""
	公众号消息回调

	:param xml: {
		XMLName      xml.Name `xml:"xml"`
		ToUserName   string
		FromUserName string
		CreateTime   string
		MsgType      string
		Event        string
		EventKey     string
		}
	:return: 可以直接回复空串，微信服务器不会对此作任何处理，并且不会发起重试
	"""
	print(XML2Dict.parsexml(request.data))

	signature = request.args.get('signature')
	timestamp = request.args.get('timestamp')
	nonce = request.args.get('nonce')
	print(signature, timestamp, nonce)

	return ''